import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter);
const layout=()=>import('../page/layout.vue')
const login=()=>import('../page/login.vue')
const menulist=()=>import('../page/menu.vue')
const routes = [
  {
    path:'/',
    redirect:'/layout',
    
  },
{
  path:'/layout',
  component:layout,
  meta:{
    islogin:true,
    title:'首页'
  },
  children:[{
    path:'/menulist',
    component:menulist,
    meta:{
      islogin:true,
      title:'首页'
    }
  }]
},
{
  path:'/login',
  component:login,
  meta:{
    islogin:false,
    title:'登录'
  }
},

]

const router = new VueRouter({
  routes
})


//编写路由守卫
//咩有token不让登录
//login不需要登录
router.beforeEach(function(to,from,next){
document.title=to.meta.title
const to_path=to.path
const no_login=['/login','/index','/list']

if(no_login.indexOf(to_path)==-1){
  const token=JSON.parse(localStorage.getItem('loginData')||'{}').token
  if(token){
    next()
  }else{
    next('/login')
  }
}

next()

})







export default router
